package com.ruoyi.project.edu.course.service.impl;

import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.project.system.att.domain.SysUpAtt;
import com.ruoyi.project.system.att.service.ISysUpAttService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.edu.course.mapper.CourseMapper;
import com.ruoyi.project.edu.course.domain.Course;
import com.ruoyi.project.edu.course.service.ICourseService;
import com.ruoyi.common.utils.text.Convert;

/**
 * 课程Service业务层处理
 *
 * @author yueqiangu
 * @date 2021-02-27
 */
@Service
public class CourseServiceImpl implements ICourseService
{
    @Autowired
    private CourseMapper courseMapper;

    @Autowired
    private ISysUpAttService sysUpAttService;

    /**
     * 查询课程
     *
     * @param id 课程ID
     * @return 课程
     */
    @Override
    public Course selectCourseById(Long id)
    {
        Course course = courseMapper.selectCourseById(id);
        SysUpAtt sysUpAtt = new SysUpAtt();
        sysUpAtt.setAttId(id+"");
        List<SysUpAtt> sysUpAttList = sysUpAttService.selectSysUpAttList(sysUpAtt);
        course.setUpAttList(sysUpAttList);
        return course;
    }

    /**
     * 查询课程列表
     *
     * @param course 课程
     * @return 课程
     */
    @Override
    public List<Course> selectCourseList(Course course)
    {
        return courseMapper.selectCourseList(course);
    }

    /**
     * 新增课程
     *
     * @param course 课程
     * @return 结果
     */
    @Override
    public int insertCourse(Course course)
    {
        course.setCreateTime(DateUtils.getNowDate());
        course.setCreateBy(ShiroUtils.getLoginName());
        int i= courseMapper.insertCourse(course);
        if(i>0){
            if(StringUtils.isNotEmpty(course.getUpAttListJosn())){
                List<SysUpAtt> upAttList = JSON.parseObject(course.getUpAttListJosn(),new TypeReference<List<SysUpAtt>>(){});
                upAttList.forEach(data->{
                    data.setAttId(course.getId().toString());
                    sysUpAttService.insertSysUpAtt(data);
                });
            }
        }
        return i;
    }

    /**
     * 修改课程
     *
     * @param course 课程
     * @return 结果
     */
    @Override
    public int updateCourse(Course course)
    {
        course.setUpdateTime(DateUtils.getNowDate());
        course.setUpdateBy(ShiroUtils.getLoginName());
        int i = courseMapper.updateCourse(course);
        if(i>0){
            sysUpAttService.deleteByAttId(course.getId().toString());
            if(StringUtils.isNotEmpty(course.getUpAttListJosn())){
                List<SysUpAtt> upAttList = JSON.parseObject(course.getUpAttListJosn(),new TypeReference<List<SysUpAtt>>(){});
                upAttList.forEach(data->{
                    data.setAttId(course.getId().toString());
                    sysUpAttService.insertSysUpAtt(data);
                });
            }
        }
        return i;
    }

    /**
     * 删除课程对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteCourseByIds(String ids)
    {
        return courseMapper.deleteCourseByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除课程信息
     *
     * @param id 课程ID
     * @return 结果
     */
    @Override
    public int deleteCourseById(Long id)
    {
        return courseMapper.deleteCourseById(id);
    }
}
